package controller;




import java.util.ArrayList;
import java.util.List;

import model.Car;
import model.CarLocation;
import view.EditorDialog;
import view.CityWidget;
import view.MenuBar;
import view.MainWindow;
import view.NewMapDialog;

import com.trolltech.qt.gui.QFileDialog;
import com.trolltech.qt.gui.QMessageBox;



public class MenuBarControl {
	
	
	private MainWindow mainWindow;
	private boolean onCity;
	private boolean play;
	
	private final List<Car> carsToAdd;
		

	public MenuBarControl(final MainWindow monkeyWindow) {
		this.mainWindow = monkeyWindow;
		carsToAdd = new ArrayList<Car>();
		play=false;
	}

	public void fileNewMapTrigger() {
		final NewMapDialog tmp = new NewMapDialog(mainWindow);	
		tmp.show();
	}
	
	public void fileOpenMapTrigger() {
		showLabWidget();
		final String path = QFileDialog.getOpenFileName(mainWindow,"Open File","./");
//		try {
//			if(!path.isEmpty())
////				mainWindow.setMbEnvironment(SaveLoadFile.load(path));
//		} catch (IOException e) {QErrorMessage.qtHandler().showMessage(e.getMessage());}
//		catch (ClassNotFoundException e) {QErrorMessage.qtHandler().showMessage(e.getMessage());}
		mainWindow.repaint();		
	}

	public void fileSaveMapTrigger() {		
		final String map = QFileDialog.getSaveFileName(mainWindow, "Save File","./");		
//		try {
//			if(!map.isEmpty())
////				SaveLoadFile.save(mainWindow.getMbEnvironment(),map);
//		} catch (IOException e) {QErrorMessage.qtHandler().showMessage(e.getMessage());}
	}
	
	public void fileExitTrigger() {
        mainWindow.close();
	}
	
	public void editorStepCarsTrigger() {
		final EditorDialog tmp = new EditorDialog(mainWindow,0);	
		tmp.show();
	}
	public void editorAddCarsTrigger() {
		disableBars(true);
		final EditorDialog tmp = new EditorDialog(mainWindow,1);	
		tmp.show();
	}

	public void setStepCarsNumber(final int i) {
		(mainWindow.getTrafficAgent()).setCarPerStep(i);	
	}
	public void setAddedCars(boolean b) {
		enableBars(true);
		mainWindow.setAddingCars(false);
		if (b&&!play) {
			//SAVE CARS
			
//			Street curPos= new Street(new Crossroads(0, 0), new Crossroads(0, 1));
//			Street destPos=new Street(new Crossroads(1, 3), new Crossroads(2, 3));
//			carsToAdd.add(new Car(curPos, 3, destPos, 3));
			
			for (final Car c : carsToAdd) {
				for (final Car test : (mainWindow.getTrafficAgent()).getMap().getCars()) {
					if(c.getCurPos().equals(test.getCurPos())&& c.getCurPosIndex()==test.getCurPosIndex()){
						QMessageBox.critical(mainWindow, "Warning", "Same Car Position");
						return ;						
					}
				}
			}
			for (final Car c : carsToAdd) {
				(mainWindow.getTrafficAgent()).getMap().getCars().add(c);				
			}
		}
		carsToAdd.clear();
		
	}
	
//	public List<Car> getCarsToAdd() {
//		return carsToAdd;
//	}

	public void editorPlayTrigger() {
		((MenuBar)mainWindow.menuBar()).getEditor().setEnabled(false);
		((MenuBar)mainWindow.menuBar()).getFile().actions().get(0).setEnabled(false);
		((MenuBar)mainWindow.menuBar()).actions().get(2).setEnabled(false);
		((MenuBar)mainWindow.menuBar()).actions().get(3).setEnabled(true);
		if(!play){
			(mainWindow.getTrafficAgent()).play();
			play=true;
		}
		
		
	}
	public void editorStopTrigger() {
		((MenuBar)mainWindow.menuBar()).getEditor().setEnabled(true);
		((MenuBar)mainWindow.menuBar()).getFile().actions().get(0).setEnabled(true);
		((MenuBar)mainWindow.menuBar()).actions().get(2).setEnabled(true);
		((MenuBar)mainWindow.menuBar()).actions().get(3).setEnabled(false);
		if(play){
			(mainWindow.getTrafficAgent()).stop();
			play=false;
		}
	}
	
	public void showLabWidget(){
		enableBars(false);
		if(onCity){
			reset();
			return;
		}
		mainWindow.setToolBarVisible();				
		mainWindow.setCentralWidget(new CityWidget(mainWindow));
//		((MenuBar)mainWindow.menuBar()).getFile().actions().get(2).setEnabled(true);			
		mainWindow.centralWidget().show();
		onCity=true;
	}

	private void disableBars(boolean fileToo) {
		if (fileToo) {
//			mainWindow.
			((MenuBar)mainWindow.menuBar()).getFile().setEnabled(false);			
		} 
		((MenuBar)mainWindow.menuBar()).getEditor().setEnabled(false);
		((MenuBar)mainWindow.menuBar()).setEnabledButtons(false);
	}

	private void enableBars(boolean fileToo) {
		if (fileToo) {
			((MenuBar)mainWindow.menuBar()).getFile().setEnabled(true);			
		} 
		((MenuBar)mainWindow.menuBar()).getEditor().setEnabled(true);
		((MenuBar)mainWindow.menuBar()).setEnabledButtons(true);
	}

	public  void reset() {	
		mainWindow.getCityMap().getCars().clear();
		//mainWindow.getTrafficAgent().setPerfMesure()    TODO 

		mainWindow.repaint();
	}
	
	public boolean canAddCarToAddingList(int x,int y){
		CarLocation c = CarLocation.carLocationFromMatrix(x,y);
		if(c==null||
				   c.getStreet().getStart()==null ||
				   c.getStreet().getEnd()==null
					){
//					QMessageBox.critical(mainWindow, "Warning", "Position Error ");
					return false;						    			
				}
		
		for (final Car test : mainWindow.getTrafficAgent().getMap().getCars()) {
			if(c.getStreet().equals(test.getCurPos())&& c.getPosition()==test.getCurPosIndex()){
				QMessageBox.critical(mainWindow, "Warning", "Same Car Position on Map");
				return false;						
			}
		}
		
		for (final Car test : carsToAdd) {
			if(c.getStreet().equals(test.getCurPos())&& c.getPosition()==test.getCurPosIndex()){
				QMessageBox.critical(mainWindow, "Warning", "Same Car Position Adding List");
				return false ;						
			}
		}
		return true;
	}

	public void addCarToAddingList(int x,int y ,int dx,int dy) {
		
		Car c = CarLocation.carFromMatrix(x, y , dx, dy);
		if(c==null||
		   c.getCurPos().getStart()==null ||
		   c.getCurPos().getEnd()==null ||
  		   c.getDestPos().getStart()==null ||
		   c.getDestPos().getEnd()==null
			){
			QMessageBox.critical(mainWindow, "Warning", "Position Error " + c);
			return ;						    			
		}
		
		canAddCarToAddingList(x, y);
		
//		for (final Car test : mainWindow.getTrafficAgent().getMap().getCars()) {
//			if(c.getCurPos().equals(test.getCurPos())&& c.getCurPosIndex()==test.getCurPosIndex()){
//				QMessageBox.critical(mainWindow, "Warning", "Same Car Position on Map");
//				return ;						
//			}
//		}
		
//		for (final Car test : carsToAdd) {
//			if(c.getCurPos().equals(test.getCurPos())&& c.getCurPosIndex()==test.getCurPosIndex()){
//				QMessageBox.critical(mainWindow, "Warning", "Same Car Position Adding List");
//				return ;						
//			}
//		}

		
		carsToAdd.add(c);
		
//		System.out.println("deve dare : 10,20,2 ");carFromMatrix(11, 1,0,0);
//		System.out.println("deve dare : 20,10,3 ");carFromMatrix(10, 0,0,0);
//		System.out.println("deve dare : 22,12,4 ");carFromMatrix(9, 14,0,0);
//		System.out.println("deve dare : 12,11,0 ");carFromMatrix(8, 13,0,0);
//		System.out.println("deve dare : 21,22,3 ");carFromMatrix(14, 12,0,0);    		
		
		
	}
		

}
